package com.codeshellme.bookmanager.service.impl;

import com.codeshellme.bookmanager.dao.UmsMenuDao;
import com.codeshellme.bookmanager.dao.UmsUserDao;
import com.codeshellme.bookmanager.dto.UmsMenu;
import com.codeshellme.bookmanager.dto.UmsUser;
import com.codeshellme.bookmanager.service.UmsMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UmsMenuServiceImpl implements UmsMenuService {

    @Autowired
    private UmsMenuDao umsMenuDao;

    @Autowired
    private UmsUserDao umsUserDao;

    @Override
    public List<Map<String, Object>> queryMenus() {
        // 目录菜单
        List<Map<String, Object>> dirMenus = umsMenuDao.queryMenus("DIR");
        // 子菜单
        List<Map<String, Object>> subMenus = umsMenuDao.queryMenus("SUBMENU");

        return packEasyUiTreeData(dirMenus, subMenus);
    }

    @Override
    public Map<String, Object> queryMenuById(Integer id) {
        return umsMenuDao.queryMenuById(id, 1);
    }

    @Override
    public List<Map<String, Object>> queryAllDirMenus() {
        return umsMenuDao.queryMenusStatus("DIR", 1);
    }

    @Override
    public int updateMenu(UmsMenu menu) {
        return umsMenuDao.updateMenu(menu);
    }

    @Override
    public int addMenu(UmsMenu menu) {
        if (menu.getParent_id() == null) {
            menu.setParent_id(0);
        }
        return umsMenuDao.addMenu(menu);
    }

    /**
     *
     * @param id   主键 id
     * @param type 菜单类型
     * @return
     *      0 删除的主键不存在
     *      1 删除成功
     *     -2 目录菜单下还有子目录
     *     -3 错误的目录类型
     */
    @Override
    public int deleteMenuById(Integer id, String type) {
        if (type.equals("DIR")) {
            // 查询是否有子目录
            List<Map<String, Object>> subMenus = umsMenuDao.querySubMenusById(id);
            if (subMenus.size() != 0) {
                return -2;
            }

            return umsMenuDao.deleteMenuById(id);

        } else if (type.equals("SUBMENU")) {
            // 直接删除
            return umsMenuDao.deleteMenuById(id);
        } else {
            return -3;
        }
    }

    // 根据用户ID，查询它所属的角色，并查询该角色的菜单
    @Override
    public List<Map<String, Object>> queryRoleMenusByUserId(Integer userId) {
        // 目录菜单
        List<Map<String, Object>> dirMenus = umsMenuDao.queryRoleMenusByUserId(userId, "DIR");
        // 子菜单
        List<Map<String, Object>> subMenus = umsMenuDao.queryRoleMenusByUserId(userId, "SUBMENU");

        return packEasyUiTreeData(dirMenus, subMenus);
    }

    @Override
    public List<Map<String, Object>> queryRoleMenusByUserName(String userName) {
        UmsUser user = umsUserDao.findByUserName(userName);
        if (user == null) {
            return new ArrayList<>();
        }

        return queryRoleMenusByUserId(user.getId());
    }

    @Override
    public List<Map<String, Object>> queryMenuUrlNeedRoleIds() {
        return umsMenuDao.queryMenuUrlNeedRoleIds();
    }

    /**
     * 组装 EasyUI 所需的 Tree 数据
     * @param dirMenus 父目录
     * @param subMenus 子菜单
     */
    private List<Map<String, Object>> packEasyUiTreeData(
            List<Map<String, Object>> dirMenus,
            List<Map<String, Object>> subMenus) {

        List<Map<String, Object>> retMenus = new ArrayList<>();

        for (Map<String, Object> dirMenu: dirMenus) {
            Map<String, Object> m = new HashMap<>();
            List<Map<String, Object>> children = new ArrayList<>();

            m.put("id", dirMenu.get("id"));
            m.put("text", dirMenu.get("name"));
            m.put("iconCls", dirMenu.get("icon"));
            m.put("children", children);
            String id = dirMenu.get("id").toString();

            for (Map<String, Object> subMenu: subMenus) {
                String parent_id = subMenu.get("parent_id").toString();
                if (!parent_id.equals(id)) {
                    continue;
                }

                Map<String, Object> mm = new HashMap<>();
                mm.put("id", subMenu.get("id"));
                mm.put("text", subMenu.get("name"));
                mm.put("iconCls", subMenu.get("icon"));
                mm.put("url", subMenu.get("url"));

                children.add(mm);
            }

            retMenus.add(m);
        }

        return retMenus;
    }
}
